#!/bin/python3
import requests as rq
import re

class ItingUtils(object):

    url = "http://music.moresound.tk/api.php?search=it4g"
    headers = {"User-Agent":"Mozilla/5.0 Windows NT 10.0; Win64; x64) AppleWebKit/537.36 KHTML,(( like Gecko ) Chrome/81.0.4044.113 Safari/537.36)", "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}

    def __init__(self):
        cookies = "XLA_CI=eae10004267d327aa73864aca4f4942c; IP=Tk9Y2ilamDyMBYcIMLThoXTtnrwjL5SikzUPG6WWY%252FU%253D; QQMUSIC_SIP=%5B%22http%3A//111.27.123.12/amobile.music.tc.qq.com/%22%2C%22http%3A//111.27.123.13/amobile.music.tc.qq.com/%22%2C%22http%3A//111.27.123.14/amobile.music.tc.qq.com/%22%5D; QQ="
        self.cookie_dict = {i.split("=")[0]:i.split("=")[-1] for i in cookies.split("; ")} 

    @classmethod
    def get_instance(cls):
        instance = getattr(cls, "_instance", None)
        if not instance:
            cls._instance = cls()
            return cls._instance
        else: return instance

    def iting_post(self, data, uri=None):
        url = self.url.split("?")[0] + "?"

        if uri: url += uri+"=it4g"
        else: url = self.url

        #if type(data) is not str: data = data.decode("utf-8")
        data = str(data)
        if re.findall(r'[\u4e00-\u9fa5]', data): data = data.encode("utf-8")
        resp = rq.post(url=url, headers=self.headers, data=data, cookies=self.cookie_dict)

        return resp.status_code, resp.json()

    def iting_get(self, uri):
        url = "http://music.moresound.tk/"+uri
        headers = {"User-Agent":self.headers.get("User-Agent")}
        resp = rq.get(url=url, headers=headers, cookies=self.cookie_dict)

        return resp.status_code, resp.json()

